package com.jiuzhou.guanwang.jzcp.fragment;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.URLConfig;
import com.jiuzhou.guanwang.jzcp.adapter.JclqAdapter;
import com.jiuzhou.guanwang.jzcp.base.BaseFragment;
import com.jiuzhou.guanwang.jzcp.bean.LqMatchInfoBean;
import com.jiuzhou.guanwang.jzcp.bean.LqMatchInfoResult;
import com.jiuzhou.guanwang.jzcp.netutil.NetRequestUtil;
import com.jiuzhou.guanwang.jzcp.utils.CommonUtils;
import com.vise.log.ViseLog;

import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.Map;

public class JclqFragment extends BaseFragment {
    private JclqAdapter mJclqAdapter;
    private ListView mJclqLv;
    private TextView mTimeTv;

    protected void initView(View view) {
        this.mTimeTv = (TextView) view.findViewById(R.id.tv_time);
        this.mJclqLv = (ListView) view.findViewById(R.id.lv_jclq);
    }

    protected int bindLayout() {
        return R.layout.fragment_jclq;
    }

    protected void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("mobileType", "android");
        map.put("channel", "qq_tab1");
        map.put("apiLevel", "27");
        map.put("apiVer", "1.1");
        map.put("ver", "4.31");
        Callback.Cancelable post = NetRequestUtil.getInstance().get(URLConfig.LQ, map, 101,
                new NetRequestUtil.NetResponseListener<String>() {
                    @Override
                    public void onSuccess(String response, int requestCode) {
                        ViseLog.d(response);
                        LqMatchInfoResult result = (LqMatchInfoResult) new Gson().fromJson(response, LqMatchInfoResult.class);
                        if (result != null && result.result == 100 && result.data.get(0) != null && !CommonUtils.isEmpty(((LqMatchInfoBean) result.data.get(0)).matchInfo)) {
                            if (mJclqAdapter == null) {
                                mJclqAdapter = new JclqAdapter(getActivity());
                                mJclqLv.setAdapter(mJclqAdapter);
                            }
                            mJclqAdapter.setData(((LqMatchInfoBean) result.data.get(0)).matchInfo);
                            mTimeTv.setText(((LqMatchInfoBean) result.data.get(0)).period);
                        }
                    }

                    @Override
                    public void onFailed(String response, int requestCode) {
                        ViseLog.e("请求失败");
                       showToast("返回失败");
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("返回解析异常");
                    }

                    @Override
                    public void onFinished() {
                    }
                });
    }
}
